<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
<title>Hello APP</title>
<link rel="stylesheet" type="text/css" href="../../css/swiper.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui-flex.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
<link rel="stylesheet" type="text/css" href="../../css/common.css" />
<style>
	.godInfo {
		margin-bottom: 0.25rem;
		background: #FFF;
		padding: 0.75rem;
	}
	.priceInfo {
		padding: 0.25rem 0;
		font-size: 0.6rem;
		color: #adadad;
		line-height: 1rem;
	}
	.priceInfo .price {
		font-size: 0.8rem;
	}
	.coupon {
		position: relative;
	}
	.couponInfo {
		position: absolute;
		left: 0;
		top: 0;
		width: 70%;
		height: 100%;
		color: #FFF;
	}
	.couponInfo .couponNum {
		font-size: 0.7rem;
		margin-bottom: 0.1rem;
	}
	.couponInfo .couponNum span {
		margin: 0 0.15rem;
		font-size: 1.5rem;
		line-height: 1;
	}
	.couponInfo .couponDate {
		font-size: 0.5rem;
	}
	.productDetail {
		background: #FFF;
	}
	.productDetail .ttl {
		font-size: 0.6rem;
		padding: 0.75rem;
	}
	.shop {
		background: #FFF;
		padding: 0.5rem;
	}
	.shop .shopIco {
		width: 1.5rem;
		height: 1.5rem;
		background: url(../../image/common/taobao.png) no-repeat center center;
		background-size: 1.5rem;
	}
	.shop .shopIco.tm {
		background: url(../../image/common/tianmao.png) no-repeat center center;
		background-size: 1.5rem;
	}
	#sharebtn {
		 position: absolute;
		 z-index:3;
		 width:100%;
		 bottom:0px;
		 background:rgb(244,182,61);
		 color:#fff;
		 padding:12px;
		 height:50px;
		 width:100%;
		 border:none
	}
	#foo2{
		display: none;
	}
</style>
</head>

<body class="bg-f5 fadePage">
	<div id="data_html" ></div>
<script type="text/x-dot-template" id="data_tpl">
	<div class="swiper-container mainImg">
		<div class="swiper-wrapper" >
			{{ if(it.small_images[0]){ }}
			{{ for(var i in it.small_images) { }}
			<div class="swiper-slide"><img src="{{= it.small_images[i] }}" width="100%" alt=""></div>
			{{ } }}
			{{ }else{ }}
			<div class="swiper-slide"><img src="{{= it.pict_url }}" width="100%" alt=""></div>
			{{ } }}
		</div>
	</div>
	<div class="godInfo" >
		<div class="godTitle"><span id="foo3" class="ttlTag">{{= it.user_type }}</span>{{= it.title }}</div>
		<div class="priceInfo aui-flex-col aui-flex-middle font-helvet">
			<div class="color-red aui-margin-r-10">券后价 <span class="price">¥{{= it.coupon_amount }}</span></div>
			<div>现价 ¥{{= it.zk_final_price }}</div>
			<div class="flex-auto aui-font-size-10 aui-text-right">月销 {{= it.volume }}件</div>
		</div>
		{{ if(it.coupon_info){ }}
		<div class="coupon" tapmode onclick="taobao();">
			<img src="../../image/best/coupon.png" width="100%" alt="">
			<div class="couponInfo aui-flex-row aui-flex-middle aui-flex-center">
				<div class="couponNum">￥<span>{{= it.coupon_info }}</span>优惠券</div>
				<div class="couponDate">使用期限 {{= Date.parse(it.coupon_start_time).toString('yyyy.M.d')}}-{{= Date.parse(it.coupon_end_time).toString('yyyy.M.d')}}</div>
			</div>
		</div>
		{{ } }}
	</div>
	<div style="position:relative;" onclick="checkrule()">
	<input type="text" style="display:none" id="foo" readonly/>
	 <button style="display:none" class="btn" id ="sharebtn"  data-clipboard-action="copy"  data-clipboard-target="#foo">分享淘口令赚佣金</button>
	<button class="btn" id ="sharebtn" onclick="openCouling()" >分享淘口令赚佣金</button>


	{{ if(it.shop_title){ }}
	<div class="shop aui-flex-col aui-flex-middle aui-flex-between aui-margin-b-10">
		<div class="shopIco {{= it.user_type=='天猫' ? 'tm' : '' }}"></div>
		<div class="aui-px-10 flex-auto">{{= it.shop_title || ''}}</div>
		<div class="color-999 aui-font-size-12">{{= it.provcity || '' }}</div>
	</div>
	{{ } }}

	</div>
	<div class="relateBox">
		<div class="ttl aui-text-center aui-py-5">相关商品</div>
		<div class="comFlowBox">
			<div class="comGodList" id="flow_html">
				<div class="grid-sizer"></div>
				{{ for(var i in it.recommend_shop){ }}
				{{ if(it.recommend_shop[i].title){ }}
				<div class="item" data-id="{{= it.recommend_shop[i].shop_id}}" tapmode onClick="openProductDetail({{= it.recommend_shop[i].shop_id}});">
					<div class="inner">
						{{ if(it.recommend_shop[i].data_amount && getUserVip()>1 ){ }}
						<div class="earn">赚￥{{= it.recommend_shop[i].data_amount}}</div>
						{{ } }}
						<div class="photo"><img src="{{= it.recommend_shop[i].pict_url}}" alt=""></div>
						<div class="textBox">
							<div class="title aui-flex-col aui-flex-middle">
								<div class="aui-ellipsis-2"><span class="tag">{{= it.recommend_shop[i].user_type }}</span>{{= it.recommend_shop[i].title }}</div>
							</div>
							<div class="aui-flex-col aui-flex-middle priceBox">
								<div class="nowPrice">¥{{= it.recommend_shop[i].coupon_amount }}</div>
								<div class="delPrice">¥<del>{{= it.recommend_shop[i].zk_final_price }}</del></div>
							</div>
							<div class="aui-flex-col aui-flex-middle aui-flex-between">
								{{ if( it.recommend_shop[i].coupon_info ){ }}
								<dl class="quan outline aui-flex-col aui-flex-middle">
									<dt>券</dt>
									<dd>{{= it.recommend_shop[i].coupon_info }}元</dd>
								</dl>
								{{ }else{ }}
								<div></div>
								{{ } }}
								<div class="sale">月销 {{= it.recommend_shop[i].volume }}件</div>
							</div>
						</div>
					</div>
				</div>
				{{ } }}
				{{ } }}
			</div>
		</div>
	</div>
	<div class="productDetail aui-hide">
		<div class="ttl">商品详情</div>
		<div class="htmlDetail">

		</div>
	</div>
</script>

</body>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/clipboard.js"></script>
<script type="text/javascript" src="../../script/import.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/swiper.min.js"></script>
<script type="text/javascript" src="../../script/date.js"></script>
<script type="text/javascript" src="../../script/imagesloaded.pkgd.min.js"></script>
<script type="text/javascript" src="../../script/masonry.pkgd.min.js"></script>
<script type="text/javascript">
var topSwiper;
var GOD_ID;
var GOD_INFO;
var gridDom = '#flow_html';
var grid;
apiready = function() {
	api.parseTapmode();
	GOD_ID = api.pageParam.shopId;
	getData();
};
//获取数据
function getData(){
	showProgress();
	var postData = {
		shop_id: GOD_ID
	}
	if( getUserId() ){
		postData.userid = getUserId();
	}
	api.ajax({
        url: BASE_URL + 'App/Select/shopDetail',
        method: 'post',
        data: {
            values: postData
        }
    },function(ret,err){
		hideProgress();
        if (ret && ret.code == 1 ) {
			GOD_INFO = ret.data;
			if(!GOD_INFO.shop_id){
				toast('该商品已删除');
				setTimeout(function(){
					api.closeWin();
				},2000);
				return;
			}
			api.sendEvent({
				name: 'godFootUpdateEvent',
				extra: {
					godInfo: GOD_INFO
				}
			});
			var tpl = $$('#data_tpl').html();
			var tempFn = doT.template(tpl);
			$$('#data_html').html(tempFn(ret.data));
			//调用瀑布流
			grid = new Masonry( gridDom, {
				columnWidth: '.grid-sizer',
				itemSelector: '.item',
				percentPosition: true
			});
			imagesLoaded( gridDom, function() {
				grid.layout();
			});
			api.parseTapmode();
			//图片切换
			sliderInit();
			//获取淘口令
			getKouling();
			//页面整体淡入
			setTimeout(function(){
				$$('.fadePage').addClass('show');
			},300);
        } else {

        }
    });
}
//图片切换
function sliderInit(){
	topSwiper = new Swiper('.mainImg',{
		speed: 600,
		autoplay: false,
		autoHeight: true
	});
}
//获取淘口令
function getKouling(){
	// console.log(JSON.stringify(GOD_INFO));
	var userInfo = getUserinfo();
	if(GOD_INFO.coupon_share_url){
		var url = "https:" + GOD_INFO.coupon_share_url
	}else {
		var url = "https:" + GOD_INFO.url
	}

	var postData = {
		url: url,
		short_title: GOD_INFO.short_title,
		pict_url: GOD_INFO.pict_url,
		userid: getUserId(),
		user_nickname: userInfo.user_nickname,
		adzone:GOD_INFO.adzone
	}
	// console.log(JSON.stringify(postData))
	api.ajax({
        url: BASE_URL + 'App/Select/modelGenerate2',
        method: 'post',
        data: {
            values:postData
        }
    },function(ret,err){
			// console.log(JSON.stringify(ret));
        if (ret && ret.code == 1 ) {
			GOD_INFO.code = ret.data.model;
			var str = GOD_INFO.short_title + ret.data.model
			$('#foo2').val(str)
			$('#foo').val(ret.data.model)
			$('#code').text(ret.data.model)
        }
    });
}
//打开分享口令页面
function openCouling(){
	api.openWin({
		name: 'kouling_win',
		url: 'kouling_win.html',
		pageParam: {
			DATA: GOD_INFO
		}
	});
}

//跳转淘宝
function taobao(){
	var systemType = api.systemType;
	var appBundle = systemType == 'ios' ? 'taobao' : 'com.taobao.taobao';
	var installed = api.appInstalled({
		sync: true,
		appBundle: appBundle
	});
	if (!installed) {
		toast('请安装淘宝app');
		return;
	}
	var taobaoUrl = '';
	if(GOD_INFO.coupon_share_url){
		taobaoUrl = 'taobao:'+ GOD_INFO.coupon_share_url;
	}else{
		taobaoUrl = GOD_INFO.item_url;
		taobaoUrl = taobaoUrl.replace(/https/, "taobao")
	}
	api.openApp({
		iosUrl: taobaoUrl,
		uri: taobaoUrl
	});
}


//鉴定淘宝授权(插入到复制分享淘口令按钮中)
function checkrule(){
	var userInfo = getUserinfo();
	console.log(JSON.stringify(userInfo));
	console.log(userInfo.special_id);
	if (userInfo.special_id) {
		// 如果已经授权成功进行复制
		tocopy()
	}else {
		api.ajax({
					    url: BASE_URL + 'app/user/check_rule',
					    method: 'post',
					    data: {
					        values: {
					            userid: userInfo.userid,
					        }
					    }
					},function(ret, err){
						if (ret.code == 0) {
							api.confirm({
							    title: '申请淘宝授权',
							    msg: '授权后购买或者分享才能产生收益',
							    buttons: ['去授权', '']
							}, function(rec, err){
							    if( rec.buttonIndex == 1  ){
										// 打开授权链接
										api.openFrame({url:ret.info});
										//定时请求服务端,查看是否授权成功
										jishi()
							    }
							});
						}else {
							tocopy()
						}
						console.log(JSON.stringify(ret))
					});
	}
}

//定时请求确认是否授权成功
function jishi() {
	var timer = setInterval(function() {
		console.log('start');
		yanzheng(timer);
},1000)
}
//验证是否授权成功
function yanzheng(aa){
	var userInfo = getUserinfo();
	api.ajax({
	    url: BASE_URL + 'app/user/check_succ',
	    method: 'post',
	    data: {
	        values: {
	            userid: userInfo.userid
	        }
	    }
	},function(ret, err){
		console.log(userInfo.special_id);
			if (ret.code == 1) {
				userInfo.special_id = ret.info;
					alert( JSON.stringify( ret.info ));
					clearInterval(aa);
			}else {
				console.log(ret.info);
			}
	});
}

//复制
function tocopy(){
	var clipboard = new Clipboard('.btn');
	clipboard.set({

	})
	clipboard.on('success', function(e) {
		api.confirm({
				title: '复制成功打开淘宝',
				msg: '',
				buttons: ['确定', '取消']
		}, function(ret, err){
				if( ret.buttonIndex == 1 ){
					 taobao()
				}else{
					return;
				}
		});
	});
	clipboard.on('error', function(e) {
		alert('复制失败');
			console.log(e);
	});
}

//分享事件
function share(type){
	var shareType = type;
	var shareUrl = 'http://taoke.maiyatang.info/wx/html/wx_tk_product.html?shopid='+GOD_INFO.shop_id+'&code='+GOD_INFO.code;
	var thumb = '';
	var shortTitle = GOD_INFO.short_title ? GOD_INFO.short_title : GOD_INFO.title;
	var title = GOD_INFO.short_title ? GOD_INFO.title : '';

	api.download({
		url: GOD_INFO.pict_url,
		savePath : api.fsDir + '/share_thumb.jpg'
	}, function(ret, err) {
		if (ret.state == 1) {
			//下载成功
			thumb = ret.savePath;

			var imageFilter = api.require('imageFilter');
			imageFilter.compress({
				img: thumb,
				quality: 0.1,
				size: {
					w: 100,
					h: 100
				},
				save: {
					imgPath: api.fsDir,
					imgName: 'share_thumb.jpg'
				}
			},function( ret, err ){
				var wx = api.require('wx');
				wx.shareWebpage({
					apiKey: '',
					scene: shareType,
					title: shortTitle,
					description: title,
					thumb: thumb,
					contentUrl: shareUrl
				}, function(ret, err) {
					if (ret.status){
						toast('分享成功');
					} else {
						toast('分享失败');
					}
				});
			});


		}
	});
}

//休眠函数
function sleep(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
		return;
	    }
}

var clipboard = new Clipboard('.btn');
clipboard.on('success', function(e) {
	toast('复制成功')

	console.log(JSON.stringify(e));
	api.confirm({
			title: '是否去淘宝购买',
			msg: '',
			buttons: ['确定', '取消']
	}, function(ret, err){
			if( ret.buttonIndex == 1 ){
				 taobao()
			}else{
				return;
			}
	});
});
clipboard.on('error', function(e) {
	alert('复制失败');
		console.log(e);
});

</script>
</html>
